﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace sp.netWebFroms
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {//gv1填充数据
            var sql = "select * from Users";
            var dt = DbHelp.GetData(sql);
            gv1.DataSource = dt;
            gv1.DataBind();
        }

        protected void gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//添加
        {
            gv1.EditIndex = -1;
            FillData();
        }

        protected void gv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv1.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)//删除
        {
            var id = GetValue(e.RowIndex, 0);
            var sql = string.Format("delete from Users where ID={0}", id);
            DbHelp.COC(sql);

            gv1.EditIndex = -1;
            FillData();
        }

        private object GetValue(int rowIndex, int COLINDEX)//单元格方法
        {
            var cell2 = gv1.Rows[rowIndex].Cells[COLINDEX];

            string value = string.Empty;
            if(cell2.Controls.Count>0)
            {
                var h = cell2.Controls[0];
                var txtBox = (TextBox)h;
                value = txtBox.Text;
            }
            else
            {
                value = cell2.Text;
            }
            return value;
        }

        protected void gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)//更新
        {
            var idString = GetValue(e.RowIndex, 0);

            var isOk = int.TryParse((string)idString, out int id);
            var ID = GetValue(e.RowIndex, 1);
            var Title = GetValue(e.RowIndex, 2);
            var Content = GetValue(e.RowIndex, 3);
            var Author = GetValue(e.RowIndex, 4);

            if (isOk)
            {
                //更新
                var sql1 = string.Format("update Articles  set Title='{1}',Content='{2}',Author='{3}', where id={0}", Title,Content,Author,ID);
                DbHelp.COC(sql1);
            }
            else
            {
                //添加
                var sql2 = string.Format("insert into Article (Title,Content,Author) values ('{1}','{2}','{3}')",Title,Content,Author);
                DbHelp.COC(sql2);
            }

            gv1.EditIndex = -1;
            FillData();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id"));
            dt.Columns.Add(new DataColumn("Title"));
            dt.Columns.Add(new DataColumn("Content"));
            dt.Columns.Add(new DataColumn("Author"));
            dt.Columns.Add(new DataColumn("Remarks"));

            foreach (GridViewRow row in gv1.Rows)
            {
                var newRow = dt.NewRow();

                newRow["Id"] = GetValue(row.RowIndex, 0);
                newRow["Title"] = GetValue(row.RowIndex, 1);
                newRow["Content"] = GetValue(row.RowIndex, 2);
                newRow["Author"] = GetValue(row.RowIndex, 3);
                newRow["Remarks"] = GetValue(row.RowIndex, 8);
                dt.Rows.Add(newRow);
            }

            dt.Rows.Add(dt.NewRow());

            gv1.DataSource = dt;
            gv1.DataBind();

            gv1.EditIndex = dt.Rows.Count - 1;


            gv1.DataSource = dt;
            gv1.DataBind();
        }
    }
    }




      

        
       

        

       
       

      

       
